package socket;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ClientGUI {
    private Socket socket;
    private JFrame frame;
    private JTextArea chatTextArea;
    private JTextField chatInputField;
    private JButton sendButton;
    private PrintWriter pw;

    public ClientGUI() {
        createGUI();
        connectToServer();
    }

    private void createGUI() {
        frame = new JFrame("Chat Client");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        chatTextArea = new JTextArea(20, 50);
        chatTextArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(chatTextArea);
        chatInputField = new JTextField(40);
        sendButton = new JButton("发送");

        sendButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String message = chatInputField.getText();
                if (!message.isEmpty()) {
                    pw.println(message);
                    chatInputField.setText("");
                    chatTextArea.append("You: " + message + "\n");
                }
            }
        });

        JPanel inputPanel = new JPanel();
        inputPanel.add(chatInputField);
        inputPanel.add(sendButton);

        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(inputPanel, BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        chatInputField.requestFocus();
    }

    private void connectToServer() {
        try {
            System.out.println("正在连接服务端...");
            socket = new Socket("localhost", 8088);
            System.out.println("成功连接服务端");

            OutputStream out = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            pw = new PrintWriter(bw, true);

            new Thread(new ServerHandler()).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ClientGUI();
            }
        });
    }

    private class ServerHandler implements Runnable {
        public void run() {
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
                String msg;
                while ((msg = br.readLine()) != null) {
                    chatTextArea.append("Server: " + msg + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
